<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function _alert_close($msg){
    exit('<script type="text/javascript" src="/static/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/static/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="/static/h-ui.admin/js/H-ui.admin.js"></script>
<script>$(function(){
    layer.msg("'.$msg.'",{icon: 2,time:2000},function() {
        layer_close();
    })});</script>');
}

function _frame_reload($msg,$url,$ico = 1){
    $str = 'window.location.reload();';
    if($url){
        $str = 'window.location.href ="'.url($url).'";';
    }
    echo '<script type="text/javascript" src="/static/lib/jquery/1.9.1/jquery.min.js"></script>
              <script type="text/javascript" src="/static/lib/layer/2.4/layer.js"></script>
              <script type="text/javascript" src="/static/h-ui/js/H-ui.min.js"></script>
              <script type="text/javascript" src="/static/h-ui.admin/js/H-ui.admin.js"></script>
              <script>$(function(){
                    layer.msg("'.$msg.'",{icon: '.$ico.',time:2000},function() {
                        '.$str.'
                    })});</script>';
    exit();
}
function _frame_reloads($msg,$url,$ico = 1){
    $str = 'parent.location.reload();';
    if($url){
        $str = 'parent.location.href ="'.url($url).'";';
    }
    echo '<script type="text/javascript" src="/static/lib/jquery/1.9.1/jquery.min.js"></script>
              <script type="text/javascript" src="/static/lib/layer/2.4/layer.js"></script>
              <script type="text/javascript" src="/static/h-ui/js/H-ui.min.js"></script>
              <script type="text/javascript" src="/static/h-ui.admin/js/H-ui.admin.js"></script>
              <script>$(function(){
                    layer.msg("'.$msg.'",{icon: '.$ico.',time:2000},function() {
                        '.$str.'
                    })});</script>';
    exit();
}

/* 校验邮政编码*/
function checkZipcode($code)
{
//去掉多余的分隔符
    $code = preg_replace("/[\. -]/", "", $code);
//包含一个6位的邮政编码
    if(preg_match("/^\d{6}$/", $code))
    {
        return true;
    }else{
        return false;
    }
}


function _layer_msg($msg, $url, $ico = 2){
    $str = 'window.location.href = "' . url($url) . '";';
    $ico == 1 && $str = 'window.parent.location.reload();layer_close();';
    exit('<script type="text/javascript" src="/static/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/static/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="/static/h-ui.admin/js/H-ui.admin.js"></script>
<script>$(function(){
        layer.msg("'.$msg.'",{icon: '.$ico.',time:2000},function() {
            ' . $str . '
        })});</script>');
}

function jump_page($url,$msg,$ico = 6){
    $str = '
       <script type="text/javascript" src="/static/lib/jquery/1.9.1/jquery.min.js"></script>
       <script type="text/javascript" src="/static/lib/layer/2.4/layer.js"></script>
       <script type="text/javascript" src="/static/h-ui/js/H-ui.min.js"></script>
       <script type="text/javascript" src="/static/h-ui.admin/js/H-ui.admin.js"></script>
        <script>
        $(function(){
            layer.msg("'.$msg.'",{icon: '.$ico.',time:2000},function() {
                parent.location.reload();  
//    parent.window.location.href ="'.$url.'";
    })});</script>';
    exit($str);
}

// 无限级分类
function get_tree($data, $pid = 0, $num = 0){
    $tree = [];
    foreach ($data as $v){
        if($v['pid'] == $pid){
            $v['title']= str_repeat('-------&nbsp',$num) . $v['title'];
            $tree[] = $v;
            $tree = array_merge($tree, get_tree($data, $v['id'], $num+1));
        }
    }
    return $tree;
}
// 权限排序
function get_trees($info,$child='child',$pid='pid',$root=0){
    $packData = $tree = [];
    foreach ($info as $data){
        $packData[$data['id']] = $data;
    }
    unset($info);
    foreach ($packData as $k=>$v){
        if($v[$pid] == $root){
            $tree[] =& $packData[$k];
        }else{
            $packData[$v[$pid]][$child][] =& $packData[$k];
        }
    }
    return $tree;
}
// 格式化时间
function editTime($data){
    foreach ($data as $k=>$v){
        $data[$k]['addTime']= date("Y-m-d H:i:s",$v['addTime']);
    }
    return $data;
}

/**
 * 单文件上传
 * @param $name 上传图片的name名称
 * @param $size 设置大小
 * @param $rule 规则
 * @param $path 路径
 * @return int|string
 */
function update_image($name,$size,$rule,$path){
    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file($name);
    if(!$file) return 0;
    // 移动到服务器的上传目录 并且使用data规则
    $info = $file->validate(['size'=>$size,'ext'=>'jpg,png,gif,jpeg'])->rule($rule)->move(ROOT_PATH.'public'.$path);
    // 跳转错误信息
    $info || jump_page('',$file->getError(),2);
    // 返回路径保存数据库
    return $path . $info->getSaveName();
}

/**
 * 多文件上传(模态框)
 * @param $name 上传图片的name名称
 * @param $size 设置大小
 * @param $rule 规则
 * @param $path 路径
 * @param $jump 跳转
 * @return array|int
 */
function update_images($name,$size,$rule,$path,$jump){
    $data = $_FILES['img'];
    if(!$data['name'][0]) return 0;
    $type = ['jpg','jpeg','png','gif'];
    foreach ($data['name'] as $k => $v){
        $a = explode('.',$v);
        in_array(end($a),$type) || _layer_msg('第'. ($k+1) .'张上传图片类型错误!',$jump);
    }
    foreach ($data['tmp_name'] as $k => $v){
        is_uploaded_file($v) || _layer_msg('第'. ($k+1) .'张上传图片行为出错!',$jump);
    }
    foreach ($data['size'] as $k => $v) {
        ($v <= $size) || _layer_msg('第' . ($k + 1) . '张上传图片过大!', $jump);
    }
    // 获取表单上传文件 例如上传了001.jpg
    $files = request()->file($name);
    $arr = [];
    foreach($files as $file) {
        // 移动到服务器的上传目录 并且使用data规则
        $info = $file->rule($rule)->move(ROOT_PATH . 'public' . DS . $path);
        // 返回路径保存数据库
        $arr[] = $path . $info->getSaveName();
    }
        return $arr;
}

/**
 * 多文件上传(非模态框)
 * @param $name 上传图片的name名称
 * @param $size 设置大小
 * @param $rule 规则
 * @param $path 路径
 * @return array|int
 */
function update_img($name,$size,$rule,$path){
    $data = $_FILES['img'];
    if($data) return 0;
    $type = ['jpg','jpeg','png','gif'];
    foreach ($data['name'] as $k => $v){
        $a = explode('.',$v);
        in_array(end($a),$type) || jump_page('','第'. ($k+1) .'张上传图片类型错误!',2);
    }
    foreach ($data['tmp_name'] as $k => $v){
        is_uploaded_file($v) || jump_page('','第'. ($k+1) .'张上传图片行为出错!',2);
    }
    foreach ($data['size'] as $k => $v){
        ($v <= $size) || jump_page('','第'. ($k+1) .'张上传图片过大!',2);
    }
    // 获取表单上传文件 例如上传了001.jpg
    $files = request()->file($name);
    $arr = [];
    foreach($files as $file) {
        // 移动到服务器的上传目录 并且使用data规则
        $info = $file->rule($rule)->move(ROOT_PATH . 'public' . DS . $path);
        // 返回路径保存数据库
        $arr[] = $path . $info->getSaveName();
    }
    return $arr;
}

// 删除图片(单文件)
function delete_image($table,$id){
    $image = db($table)->field('image')->where('id','IN', $id)->select();
    foreach ($image as $v){
        $image = ROOT_PATH.'public'.$v['image'];
        chmod($image,0777);
        unlink($image);
    }

}

// 删除图片(多文件)
function delete_images($table,$id,$image =''){
    $image = ($image ? $image : db($table)->field('image')->where('id','IN', $id)->select());
    foreach ($image as $v){
        foreach (explode('|',$v['image']) as $imgs){
            $image = ROOT_PATH.'public'.$imgs;
            chmod($image,0777);
            unlink($image);
        };
    }
}


